package com.cl.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.cl.pojo.Inandout;
import com.cl.service.impl.InandoutServiceImpl;
import com.cl.utils.MsgUtil;
import com.cl.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class InandoutController {
    @Autowired
    private InandoutServiceImpl inandoutService;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private MsgUtil msgUtil;
    @PostMapping("/inRecord")
    public String inRecord(@RequestParam("form")String form,@RequestParam("vNo")String vno){
        int vNo = Integer.parseInt(vno);
        JSONObject jsonObject = JSON.parseObject(form);
        Inandout inandout = new Inandout();
        int iNumber = Integer.parseInt((String) jsonObject.get("iInnumber"));
        if (redisUtil.get(vno)==null){
            redisUtil.set(vno,iNumber);
        }else if (iNumber<0){
            return JSON.toJSONString(msgUtil.fail("数量不能为负"));
        }else {
            int number = (int) redisUtil.get(vno);
            redisUtil.getAndSet(vno,number+iNumber);
        }
        inandout.setvNo(vNo);
        inandout.setiAdministrator((String) jsonObject.get("iAdministrator"));
        inandout.setiManufactor((String) jsonObject.get("iManufactor"));
        inandout.setiInnumber(Integer.parseInt((String) jsonObject.get("iInnumber")));
        inandout.setiIntime((String) jsonObject.get("iIntime"));
        inandout.setiNumber((Integer) redisUtil.get(vno));

        int i = inandoutService.insertInRecord(inandout);
        if (i>0){
            return JSON.toJSONString(msgUtil.success("成功"));
        }

        return JSON.toJSONString(msgUtil.fail("失败"));
    }
    @PostMapping("/outRecord")
    public String outRecord(@RequestParam("form")String form,@RequestParam("vNo")String vno){
        int vNo = Integer.parseInt(vno);
        JSONObject jsonObject = JSON.parseObject(form);
        Inandout inandout = new Inandout();
        int iOutnumber = Integer.parseInt((String) jsonObject.get("iOutnumber"));
        if (redisUtil.get(vno)==null){
            return JSON.toJSONString(msgUtil.fail("当前疫苗未入库"));
        }else if (iOutnumber>(Integer)redisUtil.get(vno)){
            return JSON.toJSONString(msgUtil.fail("库存不够"));
        }else if (iOutnumber<0){
            return JSON.toJSONString(msgUtil.fail("不能为负数"));
        }else {
            redisUtil.getAndSet(vno,((Integer)redisUtil.get(vno))-iOutnumber);
        }

        inandout.setvNo(vNo);
        inandout.setiAdministrator((String) jsonObject.get("iAdministrator"));
        inandout.setiOuttime((String) jsonObject.get("iOuttime"));
        inandout.setiOutnumber(iOutnumber);
        inandout.setiCompany((String) jsonObject.get("iCompany"));
        inandout.setiNumber((Integer) redisUtil.get(vno));

        int i = inandoutService.insertOutRecord(inandout);
        if (i>0){
            return JSON.toJSONString(msgUtil.success("成功"));
        }

        return JSON.toJSONString(msgUtil.fail("失败"));
    }

    @GetMapping("/inlibrary")
    public String inlibrary(@RequestParam("page") int page,@RequestParam("limit") int limit){
        List<Map<String, Object>> maps = inandoutService.queryAllIn((page - 1) * limit, limit);
        int size = inandoutService.countAllIn();
        HashMap<String, Object> map = new HashMap<>();
        map.put("count",size);
        map.put("code",0);
        map.put("msg","");
        map.put("data",maps);
        return JSON.toJSONStringWithDateFormat(map,"yyyy-MM-hh", SerializerFeature.WriteDateUseDateFormat);
    }

    @GetMapping("/outlibrary")
    public String outlibrary(@RequestParam("page") int page,@RequestParam("limit") int limit){
        List<Map<String, Object>> maps = inandoutService.queryAllOut((page - 1) * limit, limit);
        int size = inandoutService.countAllOut();
        HashMap<String, Object> map = new HashMap<>();
        map.put("count",size);
        map.put("code",0);
        map.put("msg","");
        map.put("data",maps);
        return JSON.toJSONStringWithDateFormat(map,"yyyy-MM-hh", SerializerFeature.WriteDateUseDateFormat);
    }
}
